/*
**  Copyright (c) 2014 Michael Liu(michael.liu.point@gmail.com).
**
**  Project: Gateway Unified Management Platform
**  File:      sysmgr.c
**  Author:  Michael
**  Date:    03/20/2014
**
**  Purpose:
**    Gateway Unified Management Platform Impletement.
*/

/* Include files. */

#include "common.h"
#include "dpmgr.h"
#include "osdep.h"
#include "pfdep.h"
#include "cmsvc.h"
#include "svcmgr.h"

/* Macro constant definitions. */

/* Type definitions. */

/* Local function declarations. */

/* Macro API definitions. */

/* Global variable declarations. */

t_log_p sysmgr_log;

void sysmgr_signal_handler(int sig)
{
	extern tsa_dev rt_button;

	sysmgr_err("System Manager Catch Signal:[%d]\n", sig);
	switch (sig) {
	case SIGUSR1:
	case SIGUSR2:
		/* Check */
		rt_button.opt(&rt_button, eBUUTON_OPT_CHECK, NULL);
		break;

	case SIGCHLD:
		/* Serivce Dead? Check It. */
		svcmgr_check();
		break;

	default:
		break;
	}
}

int main(int argc, char **argvPP)
{

	e_ret ret;

	sysmgr_log_init();

	signal(SIGTERM, sysmgr_signal_handler);
	signal(SIGUSR1, sysmgr_signal_handler);
	signal(SIGUSR2, sysmgr_signal_handler);
	signal(SIGCHLD, sysmgr_signal_handler);

	pf_dep_cfg_check();

	pf_dep_cfg_init();

	ret = svcmgr_init();
	if (ret != eRET_SUCCESS) {
		sysmgr_err("svcmgr_init() failed, ret: %d.\n", ret);
		goto out;
	}

	ret = pf_dep_sa_setup();
	if (ret != eRET_SUCCESS) {
		sysmgr_err("pf_dep_sa_setup() failed, ret: %d.\n", ret);
		goto out;
	}

	ret = pf_dep_dp_setup();
	if (ret != eRET_SUCCESS) {
		sysmgr_err("pf_dep_dp_setup() failed, ret: %d.\n", ret);
		goto out;
	}

	ret = data_path_start();
	if (ret != eRET_SUCCESS) {
		sysmgr_err("data_path_start() failed, ret: %d.\n", ret);
		goto out;
	}

	ret = sys_admin_start();
	if (ret != eRET_SUCCESS) {
		sysmgr_err("data_path_start() failed, ret: %d.\n", ret);
		goto out;
	}

	ret = cmsvc_start();
	if (ret != eRET_SUCCESS) {
		sysmgr_err("cmsvc_start() failed, ret: %d.\n", ret);
		goto out;
	}

out:
	/* Should NEVER reach here. */
	pf_dep_cfg_close();
	pf_dep_reset_cfg();

	return (ret);

}				/* End of main() */
